package com.ldk.Runnable;

import java.io.File;
import java.util.*;
import java.util.concurrent.Callable;

import com.ldk.DB.DBOpt;
import com.ldk.DataBlock.DataBlock;
import com.ldk.util.MD5;

public class DataDealRunnable implements Callable<List<String>> {

	private DataBlock dataBlock;
	private String targetHead;
	
	public DataDealRunnable(DataBlock dataBlock,String targetHead) {
		this.dataBlock = dataBlock;
		this.targetHead = targetHead;
	}
	
	@Override
	public List<String> call() throws Exception {
		List<String> allFile = dataBlock.getFile();
		List<String> changeFile = new ArrayList<String>();
		for(String s :allFile){
			//除去文件路径前面多余的部分（targetHead）
			String changeS = s.substring(targetHead.length()+1);
			//将路径的\替换成/
			if(changeS.indexOf("\\")!=-1){
				changeS = changeS.replaceAll("\\\\", "/");
			}
			
			//获取数据库中的MD5码
			DBOpt db = new DBOpt();
			String svnMd5 = null;
			try {
				svnMd5 = db.getMD5(targetHead,changeS);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//计算本地文件的MD5码
			String md5 = MD5.getMd5ByFile(new File(s));
			while(md5.length()<32){
				md5 = "0"+md5;
			}
			md5 = ("$md5 $"+md5).toLowerCase();
			//比对MD5码,判断是否发生变化
			if(svnMd5==null &&!changeS.startsWith("WebRoot/WEB-INF/classes")){
				System.out.println(changeS+":");
				changeFile.add(changeS);
			}
			else if(svnMd5!=null && !svnMd5.equals(md5)){
				System.out.println(changeS+":");
				System.out.println("\t原MD5码"+svnMd5+"\r\n\t现MD5码"+md5);
				changeFile.add(changeS);
			}
		}
		return changeFile;
	}

}
